package com.nine.shop.controller.admin;

import com.nine.shop.common.ServerResponse;
import com.nine.shop.model.Goods;
import com.nine.shop.model.Image;
import com.nine.shop.model.Type;
import com.nine.shop.service.GoodsService;
import com.nine.shop.service.ImageService;
import com.nine.shop.service.TypeService;
import com.nine.shop.utils.FileUtil;
import com.nine.shop.utils.JsonUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Auther: wangcong
 * @Date: 2019/8/25 20:37
 * @Description:后台产品管理
 */
@RestController
@RequestMapping("api/shopping/admin")
public class GoodsAdmin {
    @Resource
    private GoodsService goodsService;
    @Resource
    private TypeService typeService;
    @Resource
    private ImageService imageService;

    /**
     * @Description 新建商品
     * @Param [goods, imgUrlList]
     * @Return com.nine.shop.common.ServerResponse
     * @Author wangcong
     * @Date 2019/8/27
     * @Time 15:41
     */
    @CrossOrigin
    @RequestMapping(value = "/newgoods.do", method = RequestMethod.POST)
    public ServerResponse newGoods(Goods goods, @RequestParam(name = "imgUrlList") String imgUrlList) {
        List list = JsonUtil.str2obj(imgUrlList, List.class);
        goods.setCreateDate(new Date());
        if (goodsService.save(goods)) {
            List<Image> imageList = new ArrayList<>();
            if (!list.isEmpty()) {
                for (Object aList : list) {
                    Image image = new Image();
                    image.setGoodsId(goods.getId());
                    image.setUrl((String) aList);
                    imageList.add(image);
                }
                imageService.saveBatch(imageList);//批量插入图片表数据
            }
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByErrorMsg("新建失败");
    }

    /**
     * 上传文件
     */
    @CrossOrigin
    @RequestMapping("/uploadFile.do")//上传多个图片需要前台循环调用此接口
    public ServerResponse uploadFile(@RequestParam(value = "file") MultipartFile file) {
        String result = FileUtil.uploadFile(file);
        if (result.contains("fail:")) return ServerResponse.createByError();
        return ServerResponse.createBySuccess(result);
    }

    /**
     * @Description 修改商品
     * @Param [goods]
     * @Return com.nine.shop.common.ServerResponse
     * @Author wangcong
     * @Date 2019/8/27
     * @Time 14:02
     */
    @CrossOrigin
    @RequestMapping(value = "/editgoods.do", method = RequestMethod.POST)
    public ServerResponse editGoods(Goods goods) {
        if (goodsService.updateById(goods)) {
            return ServerResponse.createBySuccess();
        } else return ServerResponse.createByErrorMsg("更新失败");
    }


    /**
     * @Description 删除商品
     * @Param [goodsId]
     * @Return com.nine.shop.common.ServerResponse
     * @Author wangcong
     * @Date 2019/8/27
     * @Time 13:49
     */
    @CrossOrigin
    @RequestMapping(value = "/deletegoods.do", method = RequestMethod.POST)
    public ServerResponse deleteGoods(Integer goodsId) {
        if (goodsService.removeById(goodsId)) {
            return ServerResponse.createBySuccess();
        } else return ServerResponse.createByErrorMsg("删除失败");
    }


    /**
     * @Description 查询所有分类
     * @Param []
     * @Return com.nine.shop.common.ServerResponse
     * @Author wangcong
     * @Date 2019/8/27
     * @Time 13:13
     */
    @CrossOrigin
    @RequestMapping(value = "/findtype.do", method = RequestMethod.POST)
    public ServerResponse findType() {
        return ServerResponse.createBySuccess(typeService.list());
    }

    /**
     * @Description 新建分类
     * @Param [typeName]
     * @Return com.nine.shop.common.ServerResponse
     * @Author wangcong
     * @Date 2019/8/27
     * @Time 13:16
     */
    @CrossOrigin
    @RequestMapping(value = "/Newtype.do", method = RequestMethod.POST)
    public ServerResponse newType(String typeName) {
        //查询数据库中是否有此分类
        if (typeService.findByTypeName(typeName) > 0) {
            return ServerResponse.createByErrorMsg("该分类已存在");
        } else {
            Type type = new Type();
            type.setName(typeName);
            typeService.save(type);
            return ServerResponse.createBySuccess();
        }
    }

    /**
     * @Description 分页查询商品信息
     * @Param [current, size]
     * @Return com.nine.shop.common.ServerResponse
     * @Author wangcong
     * @Date 2019/8/27
     * @Time 12:40
     */
    @CrossOrigin
    @RequestMapping(value = "/findgoodspage.do", method = RequestMethod.POST)
    public ServerResponse findGoodsPage(@RequestParam(name = "current", defaultValue = "1") Integer current,
                                        @RequestParam(name = "size", defaultValue = "5") Integer size) {
        return ServerResponse.createBySuccess(goodsService.findGoodsPage(current, size));//包含图片多表查询
        // ServerResponse.createBySuccess(goodsService.searchByIdPage(current, size));
    }

    /**
     * @Description 模糊查询商品
     * @Param [goodsName]
     * @Return com.nine.shop.common.ServerResponse
     * @Author wangcong
     * @Date 2019/8/27
     * @Time 13:02
     */
    @CrossOrigin
    @RequestMapping(value = "/findgoods.do", method = RequestMethod.POST)
    public ServerResponse findGoods(String goodsName) {
        List<Goods> goodsList = new ArrayList<Goods>();
        goodsList = goodsService.findByGoodsName(goodsName);
        if (goodsList.size() == 0) {
            return ServerResponse.createByErrorMsg("商品不存在");
        } else {
            return ServerResponse.createBySuccess(goodsList);
        }
    }

    /**
     * @Description 分页分类查询商品
     * @Param [current, size, type]
     * @Return com.nine.shop.common.ServerResponse
     * @Author wangcong
     * @Date 2019/8/27
     * @Time 13:07
     */
    @CrossOrigin
    @RequestMapping(value = "/findbytype.do", method = RequestMethod.POST)
    public ServerResponse finByType(@RequestParam(name = "current", defaultValue = "1") Integer current,
                                    @RequestParam(name = "size", defaultValue = "5") Integer size,
                                    @RequestParam(name = "type", defaultValue = "1") Integer type) {

        if (type == null) return ServerResponse.createByErrorMsg("该类别不存在");
        return ServerResponse.createBySuccess(goodsService.findByTypeIdPage(current, size, type));
    }
}
